package yxy.game.pm2.command.friend;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.ServerBean;
import yxy.game.pm2.bean.area.AreaBean;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.FriendMessage;
import yxy.game.pm2.module.friend.FriendModule;
import yxy.game.pm2.module.friend.NeedFriendList;

/** 好友 接受申请 */
@MessageType(proto = Command.FriendApplicantReceive, classtype = FriendMessage.FriendApplicantReceiveRequest.class)
public class CMD_FriendApplicantReceive extends Command<FriendMessage.FriendApplicantReceiveRequest> {

	@Override
	public void execute(MessageSource messageSource, FriendMessage.FriendApplicantReceiveRequest request) {
		String playerCode = messageSource.getTag();
		String applyCode = request.getPlayerCode();

		FriendMessage.FriendApplicantReceiveResponse.Builder response = FriendMessage.FriendApplicantReceiveResponse.newBuilder();

		if (applyCode.equals(playerCode)) {
			send(messageSource, response, Result.FriendCantAddSelf);
			return;
		}
		
		FriendModule friendModule = FriendModule.get(playerCode);
		FriendModule applyModule = FriendModule.get(applyCode);
		try {
			if (friendModule.tryLock() && applyModule.tryLock()) {
				if (friendModule.vacant() <= 0) {
					// 玩家满了就不再添加
					send(messageSource, response, Result.FriendIsFullYou);// 你的好友已经满了
					return;
				}
				if (applyModule.vacant() <= 0) {
					// 玩家满了就不再添加
					send(messageSource, response, Result.FriendIsFullOppo);// 对方好友已经满了
					return;
				}
				if (friendModule.getBlack().contains(applyCode)) {
					// 申请者在玩家黑名单里
					send(messageSource, response, Result.FriendCantAddBlack);// 黑名单玩家不能加好友
					return;
				}
				if (applyModule.getBlack().contains(playerCode)) {
					// 玩家在申请者黑名单里
					send(messageSource, response, Result.FriendIsFullOppo);// 对方好友已经满了
					return;
				}

				friendModule.getApply().remove(applyCode);// 删掉申请
				friendModule.getFriend().add(applyCode);// 互加好友
				applyModule.getFriend().add(playerCode);

				friendModule.set();
				applyModule.set();

				PlayerBean playerBean = PlayerBean.get(playerCode);
				ServerBean serverBean = ServerBean.get(playerBean.getServerId());
				AreaBean areaBean = AreaBean.get(serverBean.getAreaId());

				// 更新求好友列表
				NeedFriendList needFriendList = new NeedFriendList(areaBean.getGroupId());
				needFriendList.update(friendModule);
				needFriendList.update(applyModule);

				send(messageSource, response, Result.Success);
			}
		} catch (Exception e) {
			e.printStackTrace();
			send(messageSource, response, Result.DataError);
		} finally {
			friendModule.unlock();
			applyModule.unlock();
		}
	}

	private void send(MessageSource messageSource, FriendMessage.FriendApplicantReceiveResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}